package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.CommonToolModel;
import hk.m4s.lr.repair.test.model.CommonToolModelResult;
import hk.m4s.lr.repair.test.model.DeviceModel;
import hk.m4s.lr.repair.test.model.DeviceModelResult;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.service.user.MyService;
import hk.m4s.lr.repair.test.ui.adapter.EquipmentListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.ToolKitListAdapter;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/4/18.
 * 工具箱
 */

public class ToolkitActivity extends UeBaseActivity implements VListView.IXListViewListener {
    private VListView vlist;
    private TextView tipsText;
    private Context context;
    private int pageNum = 1;
    private int pageSize = 10;
    private List<CommonToolModel> list = new ArrayList<CommonToolModel>();
    private ToolKitListAdapter equipmentListAdapter;
    private String type = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.acyivity_toolkit_list);
        context = this;
        type = getIntent().getStringExtra("type");
        hiddenFooter();
        showGoBackBtn();
        setTitleText(getIntent().getStringExtra("title"));
        findBaseView();
    }

    private void findBaseView() {
        vlist = (VListView) findViewById(R.id.vlist);
        tipsText = (TextView) findViewById(R.id.tipsText);
        vlist.setXListViewListener(this);
//        vlist.setOnItemClickListener(clickItem);
        equipmentListAdapter = new ToolKitListAdapter(context, list);
        vlist.setAdapter(equipmentListAdapter);
        getData("1");
    }

    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        // getData();
        vlist.setPullLoadEnable(true);
        pageNum = 0;
        getData("1");
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(true);
        pageNum++;
        getData("2");
    }

    public void getData(final String refreshType) {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("pageNum", pageNum);
            jsondata.put("pageSize", pageSize);
           if(SharedPreferencesUtils.getSharedPreferences(Constant.ifadmin,"").equals("2")) {
             jsondata.put("id", getIntent().getStringExtra("id"));
           }
            parmas.put("jsonText", jsondata.toString());
        } catch (JSONException e) {

        }
        MyService.commonToolList(context, parmas, new ResponseCallback<CommonToolModelResult>() {
            @Override
            public void onSuccess(CommonToolModelResult deviceModelResult) {
                onLoad();
                if (deviceModelResult.getList() != null) {
                    tipsText.setVisibility(View.GONE);
                    switch (refreshType) {
                        case "1":
                            list.clear();
                            list.addAll(deviceModelResult.getList());
                            if (deviceModelResult.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            } else {
                                vlist.showFooterView();
                                vlist.setPullLoadEnable(true);
                            }
                            break;
                        case "2":
                            list.addAll(deviceModelResult.getList());
                            if (deviceModelResult.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            }
                            break;
                    }
                    equipmentListAdapter.notifyDataSetChanged();
                } else {
                    tipsText.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                tipsText.setVisibility(View.VISIBLE);
            }
        });
    }
}
